package com.example.finalassignment.util;

import cn.hutool.jwt.JWT;

import java.nio.charset.StandardCharsets;

public class JwtUtil {

    /**
     * 密钥
     */
    private static final String key = "75395123";

    /**
     * 根据 id username password 生成token
     */
    public static String createToken(String id,String username){
        JWT jwt = JWT.create();
        jwt.setPayload("id",id);
        jwt.setPayload("username",username);

        //设置密钥
        jwt.setKey(key.getBytes(StandardCharsets.UTF_8));

        return jwt.sign();
    }

    /**
     * 验证token
     */
    public static boolean verifyToken(String token){
        return JWT.of(token).setKey(key.getBytes(StandardCharsets.UTF_8)).verify();
    }

    /**
     * 解析token
     */
    public static JWT analysis(String token){
        return JWT.of(token);
    }

    /**
     * 获取token里面的id
     */
    public static String getIdByToken(String token){
        JWT analysis = analysis(token);
        return (String) analysis.getPayload("id");
    }
}
